package org.dialMsg.Server.ServerImp;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import org.dialMsg.Topic.UrlConstants;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.dialMsg.Server.ServerImp.MyCryValue.getMyCryToken;

/**
 * @author DPF
 */
@Service
public class PostExampleHutool {
   private static final String url = UrlConstants.BASE_URL+"/api/v1/robot/crm/post_contact_log";
    public String postExample(Integer biz_id, Integer state, String mobile, Integer source, String time, String user, Integer dial_result_id) {
        if (source == 1) {
            return null;
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("biz_id", biz_id);
        jsonObject.put("cont_child_state", state);
        jsonObject.put("cont_content", "跟进备注"+time);

        // 构建 cont_attr 对象
        JSONObject cont_attr = new JSONObject();
        cont_attr.put("跟进标签", new String[]{"会到网点", "办理中"});
        cont_attr.put("是否愿意调户", "是");
        jsonObject.put("cont_attr", cont_attr.toJSONString());

        jsonObject.put("mobile", mobile);
        jsonObject.put("cont_log_source", source);
        jsonObject.put("cont_time", time);
        jsonObject.put("cont_user", user);
        jsonObject.put("dial_result_id", dial_result_id);
        String requestBody = jsonObject.toJSONString();

        String myCryToken = getMyCryToken("gaoadmin");
        if(myCryToken == null || myCryToken.isEmpty()){
            return null;
        }

        // 设置请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", myCryToken);
        headers.put("Content-Type", "application/json");

        try {
            // 发起 POST 请求
            HttpResponse response = HttpRequest.post(url)
                    .addHeaders(headers)
                    .body(requestBody)
                    .execute();

            // 获取响应体
            String responseBody = response.body();
            return responseBody;
        } catch (Exception e) {
            throw new RuntimeException("service api error: " + e.getMessage(), e);
        }
    }
}